package org.yoao.crm.business.impl;

import org.yoao.crm.bean.Role;
import org.yoao.crm.bean.Session;
import org.yoao.crm.bean.User;
import org.yoao.crm.business.service.ILoginService;
import org.yoao.crm.dao.RoleDAO;
import org.yoao.crm.dao.UserDAO;
import org.yoao.crm.struts.form.LoginForm;

public class LoginServiceImpl implements ILoginService {
	private UserDAO userDAO = new UserDAO();
	private RoleDAO roleDAO = new RoleDAO();
	
	public User Login(LoginForm form) throws Exception {
		return userDAO.findUserByName(form.getUserName());
	}

	public User Logout(Session session) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Session setSession(String sessionId, String userName, String role,
			String ip, String date) throws Exception {
		Session session = new Session();
		session.setId(sessionId);
		session.setUserName(userName);
		session.setRoleId(role);
		session.setIp(ip);
		session.setDate(date);
		return session;
	}

	@Override
	public Session setSession(String sessionId, User user, String ip,
			String date) throws Exception {
		Session session = new Session();
		session.setId(sessionId);
		session.setUserName(user.getUserName());
		session.setNickName(user.getNickName());
		session.setRoleId(user.getRole());
		session.setAccessSno(user.getAccessSno());
		session.setCompanyId(user.getCompanyId());
		session.setIp(ip);
		session.setDate(date);
		Role role = roleDAO.findRoleByRoleId(user.getRole());
		session.setRoleName(role.getRoleName());
		session.setActionPermission(role.getActionPermission());
		return session;
	}
}